package com.headwind.service;

import com.headwind.entity.HwMood;
import com.headwind.error.BusinessException;
import com.headwind.multi.datasource.annotation.TargetDataSource;
import com.headwind.multi.datasource.config.DataSourceConfig;
import com.headwind.repository.HwMoodRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author : Headwind
 * @version : 1.0
 * @date : Created in 2020/8/10
 * @description : 说说实现类
 */
@Service
public class HwMoodService {
    private HwMoodRepository moodRepository;

    @Autowired
    public HwMoodService(HwMoodRepository moodRepository) {
        this.moodRepository = moodRepository;
    }

    @Transactional(rollbackFor = BusinessException.class)
    public HwMood save(HwMood mood) {
        return moodRepository.save(mood);
    }

    @TargetDataSource(dataSource = DataSourceConfig.READ_DATASOURCE)
    public List<HwMood> findAll() {
        return moodRepository.findAll();
    }

    public List<HwMood> findByUserIdAndSort(String userId) {
        return moodRepository.findByUserId(userId, Sort.by("praiseNum"));
    }

    public List<HwMood> findByUserIdAndPage(String userId) {
        return moodRepository.findByUserId(userId, PageRequest.of(1, 10));
    }
}
